<?php
/**
 * Created by PhpStorm.
 * User: fantaixi
 * Date: 2018/11/4
 * Time: 16:26
 */

namespace app\admin\controller;


use think\Db;

class App extends Base
{
    public $table = 'app';
    public function index()
    {
        $data = Db::name($this->table)->find();

        if(!request()->isPost()){
            $this->assign('data',$data);
            return $this->fetch();
        }else{

            $model = [
                'price'=> intval(input('price')*100),
                'buy_text'=> input('buy_text'),
                'use_text'=> input('use_text'),
                'modtime'=> time(),
            ];


            if($data){
                $a = Db::name($this->table)->where('id',$data['id'])->update($model);
            }else{
                $model['addtime'] =  time();
                $a = Db::name($this->table)->insert($model);
            }
            if($a){
                $this->saveConfig();
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
        return $this->retch();
    }

    private function saveConfig()
    {
        $config = Db::name($this->table)->find();
        $data = [];

        foreach ($config as $k=>$v)
        {
            if($k =='addtime' || $k =='modtime' || $k =='id'){
                continue;
            }
            $data[$k] = $v;
        }
        //生成配置文件
        $config_file = APP_PATH . 'extra/app.php';
        create_file ( $config_file );
        arr2file ( $config_file, $data );
    }
}